//
//  StoreListViewController.m
//  iFoundIt
//
//  Created by Navid Shariat on 4/05/10.
//  Copyright 2010 QUT. All rights reserved.
//

#import "StoreListViewController.h"
#import "StoreSearchViewController.h"
#import "StoreDetailsViewController.h"
#import "Store.h"
#import "StoreManager.h"

@implementation StoreListViewController

@synthesize foundPlaces;
@synthesize currentLocationCoordinate;

- (void)dealloc {
	[foundPlaces release];
    [super dealloc]; 
}

- (void)viewDidLoad {
	[self setTitle:@"Stores List"];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark methods
-  (void) navigateToDeailsView: (Store  *) store{
/*	StoreDetailsViewController *storeDetailsViewController= [[StoreDetailsViewController alloc] initWithNibName:@"StoreDetailsViewController" bundle:nil];
	[storeDetailsViewController setStore:store];
	[[self navigationController]pushViewController:storeDetailsViewController animated:YES];
	[storeDetailsViewController release];				*/

	StoreMapViewController * storeMapViewController= [[StoreMapViewController alloc] initWithNibName:@"StoreMapViewController" bundle:nil];
	NSMutableArray*stores=[[NSMutableArray alloc] initWithObjects:store,nil];
	[storeMapViewController setFoundPlaces:stores];
	[storeMapViewController setAlternativeLocationCoordinate:[self currentLocationCoordinate]]; 	
	//might crash
	[stores release];
	//end might crash
	[[self navigationController] pushViewController:storeMapViewController animated:YES];
	[storeMapViewController release];			
	
}			


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSInteger c=[[self foundPlaces] count];
    return c;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
		[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
//		[cell setAccessoryType: UITableViewCellAccessoryDetailDisclosureButton];
    }
    
    // Set up the cell...
	NSUInteger row= [indexPath row];
	//	NSUInteger section=[indexPath section];
	Store * store= [[self foundPlaces] objectAtIndex:row];
	[[cell textLabel]setText:[store shopName]];
	[[cell detailTextLabel] setText:[NSString stringWithFormat:@"Distance: %.2lf Km", ([store distance]/1000)]];
//	[[cell imageView] setImage:[UIImage imageNamed:@"one.png"]];
    return cell;
}


#pragma mark Table view methods


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger row= [indexPath row];	
	Store * store=[foundPlaces objectAtIndex:row];
	[self navigateToDeailsView:store];
//	[store release];
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
	NSUInteger row= [indexPath row];	
	Store * store=[foundPlaces objectAtIndex:row];
	[self navigateToDeailsView:store];
	[store release];
}

@end

